package org.omgo.core.utils.generics;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * A bunch of useful generic functions
 * 
 * @author Aswin.Nair
 */
public class GenericsUtils {
	
	/**
	 * avoids having to declare SuppressWarning annotations spilled all across the source code when dealing 
	 * with legacy code.
	 * e.g
	 * 
	 * import static GenericsUtils.*;
	 * 
	 * List<Person> personList =  genericsCast(query.list());  //where query.list returns raw (pre-generics) list of Person objects.
	 *  
	 * @param <T>  The generic type to which the object k is to be assigned. Proper care must be taken for the 
	 *      safety of this call. This method does NOT check the type safety of the 
	 * @param k
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T genericsCast(Object k){
		return (T) k;
	}
	
	/**
	 * Type inferred creation of a Map, backed by a HashMap implementation.  Avoids having to repeat the whole type 
	 * information 
	 * 
	 * eg: 
	 *   import static GenericsUtils.*;
	 *    ....
	 *    
	 *   Map<String, List<Person>> personMap = newMap();
	 *  
	 *    instead of 
	 *    Map<String, Person> personMap = new HashMap<String, List<Person>>();
	 *   
	 * 
	 * @param <K>
	 * @param <V>
	 * @return
	 */
	public static <K, V> Map<K, V> newMap(){
		return new HashMap<K, V>();
	}
	
	public static <V> Set<V> newSet(){
		return new HashSet<V>();
	}
	
	public static <T> Set<T> newSet(Collection<T> list) {		
		return new HashSet<T>(list);
	}
}